package netdemo;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = new ServerSocket(10086);
        //需要接收client从传输过来的数据，需要socket对象
        Socket socket = serverSocket.accept();
        OutputStream outputStream = socket.getOutputStream();
        InputStream inputStream = socket.getInputStream();
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        PrintWriter printWriter = new PrintWriter(outputStream);
        BufferedReader con = new BufferedReader(new InputStreamReader(System.in));

        String info = null;
        String begin = null;
        System.err.println(begin = dataInputStream.readUTF());

        if (begin.equals("你好")) {
            while (!(info = con.readLine()).equals("结束")) {

                printWriter.println(info);
                printWriter.flush();
                String readinfo = null;
                readinfo = dataInputStream.readUTF();
                System.err.println(readinfo);
                if (readinfo.equals("结束")) {
                    socket.shutdownInput();
                    break;
                }
            }
        }
        if (info.equals("结束")) {
            printWriter.println(info);
            printWriter.flush();
            socket.shutdownOutput();
        }

        con.close();
        printWriter.close();
        dataInputStream.close();
        inputStream.close();
        outputStream.close();
        socket.close();
        serverSocket.close();

    }
}
